<?php

	/*
	FileTransfer File for Incoming Agencies Lista #Pontos de Pagamento#	#Puntos de Pago#
	2010-04-20
	
	*/


	#Pre-set Vars
	$ftdata = 'INSERT INTO oficcorresp
				(OFCorr, OFIDCiudad, OFDIreccion, OFMasDatos, OFCiudad, OFTelefonos1, OFTelefonos2, OFObs, OfHorSem, OfHorSab, OfHorDom, OfDelegacion, OfActiva, OfCorrCode) VALUES ';
	
	$ftdata = ' ON DUPLICATE UPDATE SET OfSyncTime = CURRENT_TIMESTAMP';
	
	CONN('MYT');
	$FILESEP = array('',',',';','	','|');	
	$fileprp[] = substr($FileList[$i],0,3);
	$fileprp[] = substr($FileList[$i],3,5);
	$ReceivedDate = substr($FileList[$i],8,8);
	$ReceivedTime = substr($FileList[$i],16,4);
	$row = 1;
	
	#gera cidades inexistentes e associa
	$insert_ciudad = 'INSERT INTO ciudades (CiuPais, CiuCiudad, Prov_ID) SELECT OFIdCiudad, OFCiudad, null FROM oficcorresp WHERE OFCorr = 314 AND OfCiudad !=  GROUP BY OFIdCiudad, OFCiudad';

	
	
	#	Valida
	$CorrProperties = $GLOBALS['MYT']->db_array("SELECT CorrNumero, CorrFormatoFichero, CorrAlias, EXT, DELIMITER, DELIMITER, OB, VERSION, LENGTH, METRIC FROM corresponsales 
													LEFT OUTER JOIN hd_fileformats ON REF = CorrFormatoFichero WHERE CorrAgente = '{$fileprp[0]}'");
													

	if(!isset($CorrProperties[1])){
		#Gerar alerta, Recebida transaccao de correspondente inexiste ou com configuracao incorrecta
		echo "Invalid transaction";
	} else {
	$CorrLastTransaction = $GLOBALS['MYT']->db_array("SELECT MAX(Op_Orden) AS LastTransactionID FROM ordenespago WHERE Op_Corr = {$CorrProperties[1]['CorrNumero']} AND Op_Modo = 2");
	# Inicia tratamento de transaccao, captados valores do nome do ficheiro
	if($CorrProperties[1]['CorrFormatoFichero'] == 7) {
	
	}
	# Verifica repeticoes de lote
	if ($CorrLastTransaction[1]['LastTransactionID']+1 != $fileprp[1]) { $Msg_ERROR = 'O numero de transaccao nao corresponde.';}	#Verificacao de Ordem
																																	#Verificacao de Data
	
	#if($FILESEP[$CorrProperties[1]['DELIMITER']]==''){$data = fgets($handle,2048);}else{$data = fgetcsv($handle,2048,$FILESEP[$CorrProperties[1]['DELIMITER']]);}
	while (($data = fgetcsv($handle,2048,';')) !== FALSE) {
		include($GLOBALS['BIB'].'FileTemplates/Input/'.$CorrProperties[1]['DELIMITER'].'_'.$CorrProperties[1]['EXT'].'_'.$CorrProperties[1]['OB'].'_'.$CorrProperties[1]['VERSION'].'.php');
	}
	fclose($handle);

	#	Regista Lote	
	CONN('MYI');
	$GLOBALS['MYI']->multi_query("CALL transact_receiveorder ({$CorrProperties[1]['CorrNumero']},0,'{$sysTime}',{$row},{$SUMTransactionA},{$SUMTransactionB},@OpID,@TransactionID); SELECT @OpID, @TransactionID");
	do {
		if ($result = $GLOBALS['MYI']->store_result()) {
				while ($row = $result->fetch_row()) {
				#Salva Ficheiro do Lote
				if($GLOBALS['MYT']->db_query(str_replace('&#TransactOrder#&',$row[0],substr($ftdata,0,strlen($ftdata)-1)))==true) {
					if($GLOBALS['MYT']->db_query("SELECT FraID,,,,,,,,,,,,,,,,, FROM facturas WHERE FraOrdenPago = ".$row[0])){
						$Msg_CONF=1;
						$GLOBALS['MYT']->db_query("UPDATE ordenespago SET Op_Enviada = 0 WHERE Op_Orden = ".$row[0]);
					}
				} 
			}
			$result->free();
		}
	} while ($GLOBALS['MYI']->next_result());
		$GLOBALS['MYI']->close();
	}
?>